<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改通知通告')"/>
    <th:block th:include="include :: summernote-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-notice-edit" th:object="${notify}">
        <input id="id" name="id" th:field="*{id}" type="hidden">
        <input id="deptUserIds" name="deptUserIds" th:field="*{deptUserIds}" type="hidden">
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">公告标题：</label>
            <div class="col-sm-10">
                <input id="notifyTitle" name="notifyTitle" th:field="*{notifyTitle}" class="form-control" type="text"
                       required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">公告类型：</label>
            <div class="col-sm-10">
                <select name="notifyType" class="form-control m-b" th:with="type=${@dict.getType('oa_notify_type')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                            th:field="*{notifyType}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">公告内容：</label>
            <div class="col-sm-10">
                <input id="notifyContent" name="notifyContent" th:field="*{notifyContent}" type="hidden">
                <div id="editor" class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">公告状态：</label>
            <div class="col-sm-10">
                <div class="radio-box" th:each="dict : ${@dict.getType('oa_notify_status')}">
                    <input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}"
                           th:field="*{status}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
                <i style="color: #d41919">注意：发布后不能进行操作。</i>
            </div>
        </div>
        <div id="recipient" class="form-group">
            <label class="col-sm-2 control-label">接收人：</label>
            <div class="col-sm-10">
                <label class="check-box">
                    <input type="checkbox" value="2" checked>展开/折叠</label>
                <label class="check-box">
                    <input type="checkbox" value="3">全选/全不选</label>
                <label class="check-box">
                    <input type="checkbox" value="4" checked>父子联动</label>
                <div id="deptUserTrees" class="ztree ztree-border"></div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<th:block th:include="include :: ztree-js"/>
<script type="text/javascript" th:inline="javascript">
    var prefix = ctx + "oa/notify";

    $(function () {
        //初始化富文本编辑器
        initSummernote();

        //初始化发布状态
        notifyStatus();

        //初始化接收人
        initDeptUserTree();

    });

    function initDeptUserTree(){
        var url = prefix + "/deptUserTreeData?userIds=" + $("#deptUserIds").val();
        var options = {
            id: "deptUserTrees",
            url: url,
            check: {enable: true},
            expandLevel: 4
        };
        $.tree.init(options);
    }

    function initSummernote(){
        $('.summernote').summernote({
            placeholder: '请输入公告内容',
            height: 320,
            lang: 'zh-CN',
            followingToolbar: false,
            dialogsInBody: true,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
        });
        var content = $("#notifyContent").val();
        $('#editor').summernote('code', content);
    }

    function notifyStatus() {
        if($(':radio[name=status]:checked').val() == '0'){
            $("#recipient").hide();
        }
        $('input').on('ifChecked', function (event) {
            var status = $(event.target).val();
            if (status == "0") {//草稿
                $("#recipient").hide();
            } else if (status == "1") {//发布
                $("#recipient").show();
            }
        });
    }

    $('input').on('ifChanged', function (obj) {
        var type = $(this).val();
        var checked = obj.currentTarget.checked;
        if (type == "2") {
            if (checked) {
                $._tree.expandAll(true);
            } else {
                $._tree.expandAll(false);
            }
        } else if (type == "3") {
            if (checked) {
                $._tree.checkAllNodes(true);
            } else {
                $._tree.checkAllNodes(false);
            }
        } else if (type == "4") {
            if (checked) {
                $._tree.setting.check.chkboxType = {"Y": "ps", "N": "ps"};
            } else {
                $._tree.setting.check.chkboxType = {"Y": "", "N": ""};
            }
        }
    })

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    $("#form-notice-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#notifyContent").val(sHTML);

            var data = $('#form-notice-edit').serializeArray();
            //获取选中的节点
            var userIds  = $.tree.getCheckedNodes();

            //如果是发布状态，提示选择接收人
            if($(':radio[name=status]:checked').val() == '1'){
                if(userIds==''){
                    $.modal.alertError("请选择接收人！");
                    return false;
                }
            }

            data.push({"name": "userIds", "value": userIds});
            $.operate.save(prefix + "/edit", data);
        }
    }
</script>
</body>
</html>
